# LuckPay Web Manage System
#
# Copyright (c) 2016 Lucky Byte, Inc.
#
express = require 'express'
pgsql   = require 'pg'
secure  = require '../lib/secure'
router = express.Router()
module.exports = router

router.get '/', (req, res, next) ->
    res.render 'upgrade'

router.post '/do', (req, res, next) ->
    await pgsql.connect settings.pgsql_url, defer err, client, done
    if err then done(client); return next(err)

    await client.query \
        "select * from pay_merch where uuid in (
            select t60_merid from pay_p2
        )", defer err, result
    if err then done(client); return next(err)

    cnt = result.rows.length

    for mer in result.rows
        trctl = mer.trctl
        trctl.trcodes.t22 = true

        await client.query \
            "update pay_merch set trctl = $1 where uuid = $2", [
                JSON.stringify(trctl), mer.uuid
            ], defer err, result
        if err then done(client); return next(err)

    done()
    res.render 'upgrade_done', cnt: cnt
